/*
 * test_renderer.cpp
 *
 *  Created on: 08-Jan-2012
 *      Author: cksingamala
 */

#include "gl_inc.h"

#include "gizmo.h"
#include "camera.h"

#include "bmp_loader.h"
#include "v3d_defs.h"
#include "v3d_renderer.h"

#include "model_data/model_data.h"

#include "light.h"

ModelData *md = 0;

Gizmo gizmo(5);

Camera camera;



struct Vertex1
{
    float tu, tv;
    float x, y, z;
};

Vertex1 g_quadVertices[] =
{
    { 0.0f,0.0f, -1.0f,-1.0f, 0.0f },
    { 1.0f,0.0f,  1.0f,-1.0f, 0.0f },
    { 1.0f,1.0f,  1.0f, 1.0f, 0.0f },
    { 0.0f,1.0f, -1.0f, 1.0f, 0.0f }
};

void DrawGizmo(Gizmo *gizmo)
{
	//TEXTURE->BindTexture(0);
	glInterleavedArrays( GL_C4UB_V3F, 0, gizmo->GetVertexArray() );
	glDrawArrays( GL_LINES, 0, 6 );
}

void TestLightMaterialInit()
{
	RENDERER->lightMgr->AddGlobalAmbientLight(&glm::vec4(0.0,0.0,0.0,1.0));
	RENDERER->lightMgr->GlobaleAmbientLight(true);

	RENDERER->SetSurfaceMaterialProperty(
		&glm::vec4(0.7, 0.7, 0.7, 1.0),
		&glm::vec4(0.1, 0.5, 0.8, 1.0),
		&glm::vec4(1.0, 1.0, 1.0, 1.0),
		&glm::vec2(100.0,0.0),
		&glm::vec4(0.3, 0.2, 0.2, 0.0),
		true		
		);

	Light *light = RENDERER->lightMgr->AddLight(
		&glm::vec3(-1.5f, 1.0f, -4.0f),
		&glm::vec4(0.0f, 0.0f, 0.0f, 1.0f ),
		&glm::vec4(1.0f, 1.0f, 1.0f, 1.0f),
		&glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));

	RENDERER->lightMgr->Enable(light);


}

void TestRendererInit()
{
#ifdef WIN32
	md = ModelDataLoad("../../resource/model/chetaah.obj");
#else
	md = ModelDataLoad("resource/model/chetaah.md");
#endif


	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	gluPerspective( 45.0, 640.0 / 480.0, 0.1, 1000.0);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);				// Black Background
	glEnable( GL_TEXTURE_2D );
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations

	TestLightMaterialInit();
}


float rotate = 0.0f;

void TestRendererUpdate()
{
	/*
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity();
	glTranslatef( 0.0f, 0.0f, -3.0f );

	glEnable(GL_TEXTURE_2D);

	glInterleavedArrays( GL_T2F_V3F, 0, g_quadVertices );
	glDrawArrays( GL_QUADS, 0, 4 );
*/
	camera.UpdateCameraInput(0.001f);

	glMatrixMode( GL_MODELVIEW );

	 glLoadIdentity();
	//glTranslatef(0.0f,0.0f,-10.0f);

	camera.GetViewMatrix();

	glMultMatrixf( camera.GetViewMatrix() );

//	DrawGizmo(&gizmo);

	rotate += 0.50f;
	if(rotate > 360)
		rotate -= 360.0f;

	//rotate = -90;
	glRotatef(rotate,0,1,0);	
	glScalef(0.05f,0.05f,0.05f);
//	glInterleavedArrays( GL_T2F_V3F, 0, g_quadVertices );
//	glDrawArrays( GL_QUADS, 0, 4 );

	RENDERER->Render(md);


}
